package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Collar;
import com.zzld.modules.autoinn.entity.Coupon;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.service.ICollarService;
import com.zzld.modules.autoinn.service.IConponService;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.security.ShiroUser;

@Controller
@RequestMapping("/autoinn/customer")
public class CustomerController {

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IConponService couponService;

	@Autowired
	private ICollarService collarService;

	private String listurl = "/modules/autoinn/customer/customerList";
	private String cururl = "/modules/autoinn/customer/customercru";

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) {
		return listurl;
	}

	// 方法优惠券
	@RequestMapping(value = "cofang")
	public String cru(Model model, HttpServletRequest request) {
		String customerId = request.getParameter("id");
		String openid = request.getParameter("openid");
		String wxaccount = request.getParameter("wxaccount");
		model.addAttribute("customerId", customerId);
		model.addAttribute("openid", openid);
		model.addAttribute("wxaccount", wxaccount);
		return cururl;
	}

	@RequestMapping(value = "/addcoupon/pro", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON add(Model model, HttpServletRequest request)
			throws Exception {
		CommonJSON json = new CommonJSON();
		String con = request.getParameter("cont") == null ? "0" : request
				.getParameter("cont");
		int d = Integer.parseInt(con);
		Collar c = new Collar();

		
		c.setCollarTime(new Date());
		c.setCounponId(request.getParameter("couponid"));
		c.setCollarTime(new Date());
		c.setCustomerId(request.getParameter("customerId"));
		c.setOpenid(request.getParameter("openid"));
		c.setState(0);
		c.setWxaccount(request.getParameter("wxaccount"));

		//String wq = c.getCounponId();
		// 判断发放数量

		Coupon p = couponService.selectByPrimaryKey(c.getCounponId());

		if (d > 0) {
			for (int i = 0; i < d; i++) {
				List<Collar> list = collarService.selectbycouid(c
						.getCounponId());
				c.setId(UuidUtil.uuid().toString());
					// 批量发放优惠券
					if (p.getCouponCount() > list.size()) {
						try {
							collarService.saveSelective(c);
							json.setMessage(MessageUtils.ADD_SUCCESS);
							json.setSuccess(true);
						} catch (Exception e) {
							json.setMessage(MessageUtils.ADD_FAIL);
							json.setSuccess(false);
						}
					} else {
						List<Collar> list1 = collarService.selectbycouopen(
								c.getCounponId(), c.getOpenid(),
								c.getCustomerId());
						json.setMessage("优惠券已发放完,该用户获得优惠券" + list1.size() + "张");
						break;
					}
				
			}
		} else {
			json.setMessage("最少发放一张！");
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value = "/treegrids")
	@ResponseBody
	public List<TreeGrid> treegird(Model model, HttpServletRequest request) {
		List<TreeGrid> list = couponService.treeGrid();
		return list;
	}

	@RequestMapping(value = "exportXls")
	public String exportXls(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("nickname", request.getParameter("nickname"));
		map.put("vipNum", request.getParameter("vipNum"));
		map.put("level", request.getParameter("level"));
		map.put("starinnertime", request.getParameter("starinnertime"));
		map.put("endinnertime", request.getParameter("endinnertime"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", request.getAttribute("order"));

		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();

		List<Customer> list = customerService.selall();
		modelMap.put(NormalExcelConstants.FILE_NAME, "消费者管理信息表");
		modelMap.put(NormalExcelConstants.CLASS, Customer.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("消费者管理信息表",
				"导出人:" + userq.getName(), "导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, list);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;

	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		try {
			String id = request.getParameter("id");
			if (customerService.deleteByPrimaryKey(id) > 0) {
				json.setMessage(MessageUtils.DELETE_SUCCESS);
				json.setSuccess(true);
			} else {
				json.setMessage(MessageUtils.DELETE_WARNING);
				json.setSuccess(false);
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}

		return json;
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public CommonJSON lists(Model model, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("nickname", request.getParameter("nickname"));
		map.put("vipNum", request.getParameter("vipNum"));
		map.put("level", request.getParameter("level"));
		map.put("starinnertime", request.getParameter("starinnertime"));
		map.put("endinnertime", request.getParameter("endinnertime"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", request.getAttribute("order"));
        map.put("phoneNum",request.getParameter("phoneNum"));
		
		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));

		CommonJSON json = new CommonJSON();
		PageInfo pageinfo = customerService.selectpage(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		return json;
	}
	
	@RequestMapping(value = "sellist", method = RequestMethod.POST)
	@ResponseBody
	public List<TreeGrid> serlist(HttpServletRequest request) {
		return customerService.selcustomer();
	}

}
